package com.spsy.order.controller;

import com.alibaba.fastjson2.JSON;
import com.spsy.api.pay.PayApi;
import com.spsy.common.core.domain.R;
import com.spsy.common.core.utils.poi.ExcelUtil;
import com.spsy.common.core.web.controller.BaseController;
import com.spsy.common.core.web.domain.AjaxResult;
import com.spsy.common.core.web.page.TableDataInfo;
import com.spsy.common.log.annotation.Log;
import com.spsy.common.log.enums.BusinessType;
import com.spsy.common.security.annotation.RequiresPermissions;
import com.spsy.order.domain.Order;
import com.spsy.order.service.IOrderService;
import com.spsy.vo.AddOrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * 订单主Controller
 * 
 * @author 翟亮超
 * @date 2025-09-22
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController extends BaseController
{

    @Autowired
    private PayApi payApi;

    @Autowired
    private IOrderService iOrderService;

    @PostMapping(value = "/create",consumes = MediaType.APPLICATION_JSON_VALUE)
    public R createOrder(@RequestBody AddOrderVo addOrderVo) throws Exception{

        log.info("订单系统-参数：addOrderVo：{}", com.alibaba.fastjson2.JSON.toJSONString(addOrderVo));

        //入数据库的
        int count = iOrderService.insertOrder(Order.builder().orderNo(UUID.randomUUID().toString()).orderStatus(1).build());

        R r = payApi.createOrder(addOrderVo);


        // TODO 发一个短信给用户，你下单成功了，赶紧支付，要不活动结束 3S。
        //{发送短信消息给用户}

        log.info("r:{}", JSON.toJSONString(r));


        return r;

    }

    @Autowired
    private IOrderService orderService;



    /**
     * 查询订单主列表
     */
    @RequiresPermissions("order:order:list")
    @GetMapping("/list")
    public TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单主列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "订单主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "订单主数据");
    }

    /**
     * 获取订单主详细信息
     */
    @RequiresPermissions("order:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderService.selectOrderById(id));
    }

    /**
     * 新增订单主
     */
    @RequiresPermissions("order:order:add")
    @Log(title = "订单主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order)
    {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改订单主
     */
    @RequiresPermissions("order:order:edit")
    @Log(title = "订单主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单主
     */
    @RequiresPermissions("order:order:remove")
    @Log(title = "订单主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderService.deleteOrderByIds(ids));
    }
}
